<?php
/**
 * RedXenon Framework
 * 
 * LICENSE
 * 
 * This source file is subject to the New BSD license that is bundled
 * with this package in the file LICENSE.txt
 * A copy of this license can be found in the world-wide-web at this URL:
 * http://www.redxenon.com/license/new-bsd-license.txt
 */

/* Abstract Class */
require_once("RXQuery.php");

/** 
 * RedXenon Database (RXUpdate.php)
 * Class to manage Database Update Queries
 * 
 * @category RedXenon
 * @package RedXenon_Database
 * @copyright Copyright (c) 2011 Red Xenon (http://www.redxenon.com)
 * @version $Id: RXUpdate.php 1.0 2011-09-06 $ 
 */
class RXUpdate extends RXQuery{

	private $fields;	
	
	/**
	 * Constructor that calls the parent class
	 * @param RXDatabaseConnection $connection
	 * @param String $query
	 * @see RXQuery::__construct
	 */
	public function __construct($connection = NULL, $query = NULL){
		$this->fields = array();
		parent::__construct($connection, $query);
	}
	
	/**
	 * Destructor
	 */
	public function __destruct(){
		parent::__destruct();
	}
	
	/**
	 * Returns the array of fields from the instance
	 * @return Array
	 */
	public function getFields(){
		return $this->fields;
	}
	
	/**
	 * Adds a field to the array that will be processed later
	 * @param String $field
	 * @param Mixed $value
	 * @param String $tableName
	 */
	public function addField($field, $value, $tableName = NULL){
		if(is_null($tableName)){
			array_push($this->fields, array("field" => $field, "value" => "'".$value."'"));			
		}else{
			array_push($this->fields, array("field" => $tableName.".".$field, "value" => "'".$value."'"));
		}		
	}
	
	/**
	 * Adds an update for a selected field that will increment its value 
	 * @param String $field
	 * @param String $tableName
	 * @param Integer $value
	 */
	public function addIncrementalValue($field, $tableName = NULL, $value = 1){
		if(is_null($tableName)){
			array_push($this->fields, array("field" => $field, "value" => $field." + ".$value));
		}else{
			array_push($this->fields, array("field" => $tableName.".".$field, "value" => $field." + ".$value));
		}
	}
	
	/**
	 * Adds an update for a selected field that will decrement its value 
	 * @param String $field
	 * @param String $tableName
	 * @param Integer $value
	 */
	public function addDecrementalValue($field, $tableName = NULL, $value = 1){
		if(is_null($tableName)){
			array_push($this->fields, array("field" => $field, "value" => $field." - ".$value));
		}else{
			array_push($this->fields, array("field" => $tableName.".".$field, "value" => $field." - ".$value));
		}
	}
	
	public function buildQuery(){
		/* UPDATE */
		$this->query = "UPDATE ";
		
		/* TABLES */
		if(is_array($this->tables)){
			foreach($this->tables as $table){
				$this->query .= $table["tableName"];
				if($table["as"]){
					$this->query .= " AS ".$table["as"];
				}
				$this->query .= ", ";
			}
			$this->query = substr($this->query, 0, strlen($this->query) - 2);
		}else{
			$this->query .= $this->tables;
		}
		
		/* SET */
		$this->query .= " SET ";
		
		/* FIELDS - VALUES */
		if(is_array($this->fields)){
			foreach($this->fields as $field){
				$this->query .= $field["field"]." = ".$field["value"].", ";
			}
			$this->query = substr($this->query, 0, strlen($this->query) - 2);
		}
		
		/* WHERE */
		if($this->where){
			$this->query .= " WHERE ".$this->where;
		}
	}
	
	public function execute(){		
		if(!$this->query)
			$this->buildQuery();
		
		if(parent::exec())
			return TRUE;
		else
			return FALSE;		
	}
}

?>